package com.studycloud.tjshareuniversity.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tojoy.shop.TJShopEngine;

/**
 * @author zhangpeng
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, "wxd8d08b194b093706", true);
        api.registerApp("wxd8d08b194b093706");
        try {
            boolean result = api.handleIntent(getIntent(), this);
            if (!result) {
               // ToastUtils.INSTANCE.showSingleToast("参数不合法，未被SDK处理，退出");
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
            handleWxShare(baseResp);
        } else if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProgram = (WXLaunchMiniProgram.Resp) baseResp;
			TJShopEngine.payCallback(WXEntryActivity.this, launchMiniProgram.extMsg);
            finish();
		}
    }

    /**
     * 处理微信分享
     *
     * @param baseResp 返回体
     */
    private void handleWxShare(BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                //ToastUtils.INSTANCE.showSingleToast("分享失败");
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                //ToastUtils.INSTANCE.showSingleToast("分享取消");
                break;
            case BaseResp.ErrCode.ERR_OK:
                //ToastUtils.INSTANCE.showSingleToast("分享成功");
                break;
            default:
                break;
        }
        finish();
    }
}